package com.example.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Redis.RedisUtils;
import com.example.common.constant.RedisConstant;
import com.example.system.domain.Item;
import com.example.system.domain.Project;
import com.example.system.domain.User;
import com.example.system.mapper.ProjectMapper;
import com.example.system.mapper.UserMapper;
import com.example.system.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CountServiceImpl implements CountService {
    String key = RedisConstant.getList_Item;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Map<String, Object> getProjectLine() {
        long l1 = System.currentTimeMillis();
        List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<>());
        List<Item> items = (List<Item>) redisUtils.get(key);
        List<Item> list = items.stream().sorted(Comparator.comparing(Item::getCreateTime)).collect(Collectors.toList());
        Item first = list.get(0);//第一个
        Item last = list.get(list.size() - 1);//最后一个
        int end_year = Integer.parseInt(last.getCreateTime().toString().split("-")[0]);
        int end_mouth = Integer.parseInt(last.getCreateTime().toString().split("-")[1]);
        List<String> types = projects.stream().map(Project::getType).distinct().collect(Collectors.toList());
        Map<String, List<Item>> list_groupBy_type = items.stream().collect(Collectors.groupingBy(item -> item.getProject().getType()));
        List<List<Long>> data = new ArrayList<>();
        List<String> x_ = new ArrayList<>();//横坐标
        List<String> finalX_ = x_;
//        list_groupBy_type.values().stream().peek(itemList -> {
//            int start_year = Integer.parseInt(first.getCreateTime().toString().split("-")[0]);
//            int start_mouth = Integer.parseInt(first.getCreateTime().toString().split("-")[1]);
//            List<Long> list1 = new ArrayList<>();
//            while (start_year < end_year || (start_year == end_year && start_mouth <= end_mouth)) {
//                String x = start_year + "-" + start_mouth;
//                finalX_.add(x);
//                long count = itemList.stream().filter(o -> o.getCreateTime().toString().startsWith(x)).count();
//                list1.add(count);
//                start_mouth++;
//                if (start_mouth == 13) {
//                    if (start_year == end_year) break;
//                    start_mouth = 1;
//                    start_year++;
//                }
//            }
//            data.add(list1);
//        }).collect(Collectors.toList());
        list_groupBy_type.forEach((k, v) -> {
            int start_year = Integer.parseInt(first.getCreateTime().toString().split("-")[0]);
            int start_mouth = Integer.parseInt(first.getCreateTime().toString().split("-")[1]);
            List<Long> list1 = new ArrayList<>();
            while (start_year < end_year || (start_year == end_year && start_mouth <= end_mouth)) {
                String x = start_year + "-" + start_mouth;
                finalX_.add(x);
                long count = v.stream().filter(o -> o.getCreateTime().toString().startsWith(x)).count();
                list1.add(count);
                start_mouth++;
                if (start_mouth == 13) {
                    if (start_year == end_year) break;
                    start_mouth = 1;
                    start_year++;
                }
            }
            data.add(list1);
        });//2265
        long l2 = System.currentTimeMillis();
        long l3 = l2 - l1;
        Map<String, Object> map = new HashMap<>();
        //横坐标
        x_ = finalX_.stream().distinct().collect(Collectors.toList());
        map.put("x", x_);
        //折线数据
        map.put("data", data);
        //项目种类
        map.put("type", types);
        return map;
    }

    @Override
    public List<Map<String, Integer>> getUserInsert() {
        Map<Integer, List<User>> map = userMapper.getUserList().stream().collect(Collectors.groupingBy(user -> Integer.parseInt(user.getCreateTime().toString().split("-")[0])));
        Map<Integer, Integer> map1 = new HashMap<>();
        map.entrySet().stream().peek(o -> {
            map1.put(o.getKey(), o.getValue().size());
        }).collect(Collectors.toList());
        TreeMap<Integer, Integer> treeMap = new TreeMap<>(map1);
        List<Map<String, Integer>> list = new ArrayList<>();
        treeMap.forEach((k, v) -> {
            Map<String, Integer> m = new HashMap<>();
            m.put("year", k);
            m.put("count", v);
            list.add(m);
        });
        return list;
    }
}
